package org.hk.kg.finance.stutype.service;

import java.util.List;
import java.util.Map;

import org.hk.kg.db.DBContext;
import org.hk.kg.ui.KGUiContext;
import org.hk.kg.utils.Constants;


public class StuTypeService implements IStuTypeService {
	private static final String STU_TYPE_INRT_SQL = "insert into stu_type(typeName,typeDesc,author)values('%s','%s','%s')",
		STU_TYPE_QUERY_SQL = "select typeId,typeName,typeDesc,author from stu_type",
		STU_TYPE_DELETE_SQL = "delete from stu_type where typeId=%s",
		GET_STU_TYPE_BYID_SQL = "select typeId,typeName,typeDesc,author from stu_type where typeId=%d",
		STU_TYPE_UPT_SQL = "update stu_type set typeName='%s',typeDesc='%s' where typeId=%d";
	public boolean createStuType(String typeName, String desc) {
		int rlt = DBContext.instatnce.execute(Constants.DB_DEFAULT,
				String.format(STU_TYPE_INRT_SQL, typeName,desc,KGUiContext.instance.currentUsr()));
		return rlt == 1;
	}
	public List<Map> queryStuTypes() {
		return DBContext.instatnce.query(Constants.DB_DEFAULT, STU_TYPE_QUERY_SQL);
	}
	public boolean deleteStuTypes(Object[] typeIds) {
		String[] sqls = new String[typeIds.length];
		for(int typeIdIdx = 0 ; typeIdIdx < typeIds.length ;typeIdIdx++)
		{
			sqls[typeIdIdx] = String.format(STU_TYPE_DELETE_SQL, typeIds[typeIdIdx]);
		}
		return DBContext.instatnce.batchExecute(Constants.DB_DEFAULT, sqls) == 1;
	}
	public Map getStuTypeById(int typeId) {
		String sql = String.format(GET_STU_TYPE_BYID_SQL, typeId);
		return DBContext.instatnce.querySingle(Constants.DB_DEFAULT, sql);
	}
	public boolean modifyStuType(int typeId, String typeName, String typeDesc) {
		String sql = String.format(STU_TYPE_UPT_SQL, typeName,typeDesc,typeId);
		return DBContext.instatnce.execute(Constants.DB_DEFAULT, sql)==1;
	}
}
